#include <iostream>
#include "httplib.h"
#include "./request/add_contacts_request.pb.h"
#include "./request/del_contacts_requext.pb.h"
#include "./request/find_one_contacts_request.pb.h"
#include "./response/add_contacts_response.pb.h"
#include "./response/del_contacts_response.pb.h"
#include "./response/find_all_contacts_response.pb.h"
#include "./response/find_one_contacts_response.pb.h"
#include "../server/ContactsServer.h"
#include "../common/ContactException.h"

using namespace httplib;

int main()
{
    std::cout << "--------服务器启动--------" << std::endl;
    Server server;//创建服务器对象
    //ContactsServer::
    ContactsServer contactsServer;

    //添加联系人
    server.Post("/contacts/add",[contactsServer](const Request& req, Response& res){
        add_contact_req::AddContactsRequest  request;
        add_contact_resp::AddContactResponse response;
        
        try{
            //反序列化
            if(!request.ParseFromString(req.body))
            {
                throw ContactException("add_request.ParseFromString error!");
            }
            //处理请求
            //contactsServer.add(request, &response);
            contactsServer.add(request,&response);
            //序列化
            std::string response_str;
            if(!response.SerializeToString(&response_str))
            {
                throw ContactException("add_response.SerializeToString error!");
            }
            res.body = response_str;
            res.set_header("Content-Type", "application/protobuf");
            res.status = 200;
            //发送
        }
        catch(ContactException& e)
        {
            std::cerr << "---->/contacts/add 发生异常!!!!" << std::endl
            << "---->异常信息:" << e.what() << std::endl;
            res.status = 500;
            base_response::BaseResponse* baseResponse = response.mutable_base_resp();
            baseResponse->set_success(false);
            baseResponse->set_error_desc(e.what());
            std::string response_str;
            if(response.SerializeToString(&response_str))
            {
                res.body = response_str;
                res.set_header("Content-Type", "application/protobuf");
            }
        }
    });

    //删除联系人
    server.Post("/contacts/del",[contactsServer](const Request& req,Response& res){
        del_contact_req::DelContactRequest request;
        del_contact_resp::DelContactResponse response;
        try{
            //反序列化
            if(!request.ParseFromString(req.body))
            {
                throw ContactException("del_request.ParseFromString error!");
            }
            //处理
            contactsServer.del(request,&response);
            //序列化
            std::string response_str;
            if(!response.SerializeToString(&response_str))
            {
                throw ContactException("del_response.SerializeToString error!");
            }
            res.body = response_str;
            res.set_header("Content-Type", "application/protobuf");
            res.status = 200;
        }catch(ContactException& e){
            std::cerr << "---->/contacts/del 发生异常!!!" << std::endl
            << "---->异常信息:" << e.what() << std::endl;
            res.status = 500;
            base_response::BaseResponse* baseResponse = response.mutable_base_resp();
            baseResponse->set_error_desc(e.what());
            baseResponse->set_success(false);
            std::string response_str;
            if(response.SerializeToString(&response_str))
            {
                res.body = response_str;
                res.set_header("Content-Type", "application/protobuf");
            }
        }
    });

    //查询某一个联系人信息
    server.Post("/contacts/del",[contactsServer](const Request& req,Response& res){
        find_one_contact_req::FindOneContactRequest request;
        find_one_contact_resp::FindOneContactResponse response;
        try{
            //反序列化
            if(!request.ParseFromString(req.body))
            {
                throw ContactException("find_one_request.ParseFromString error!");
            }
            //处理
            contactsServer.find_one(request,&response);
            //序列化
            std::string response_str;
            if(!response.SerializeToString(&response_str))
            {
                throw ContactException("del_response.SerializeToString error!");
            }
            res.body = response_str;
            res.set_header("Content-Type", "application/protobuf");
            res.status = 200;
        }catch(ContactException& e){
            std::cerr << "---->/contacts/del 发生异常!!!" << std::endl
            << "---->异常信息:" << e.what() << std::endl;
            res.status = 500;
            base_response::BaseResponse* baseResponse = response.mutable_base_resp();
            baseResponse->set_error_desc(e.what());
            baseResponse->set_success(false);
            std::string response_str;
            if(response.SerializeToString(&response_str))
            {
                res.body = response_str;
                res.set_header("Content-Type", "application/protobuf");
            }
        }
    });


    //查询全部联系人信息
    server.Get("/contacts/del",[contactsServer](const Request& req,Response& res){
        find_all_contacts_resp::FindAllContactsResponse response;
        try{
            //处理
            contactsServer.find_all(&response);
            //序列化
            std::string response_str;
            if(!response.SerializeToString(&response_str))
            {
                throw ContactException("del_response.SerializeToString error!");
            }
            res.body = response_str;
            res.set_header("Content-Type", "application/protobuf");
            res.status = 200;
        }catch(ContactException& e){
            std::cerr << "---->/contacts/del 发生异常!!!" << std::endl
            << "---->异常信息:" << e.what() << std::endl;
            res.status = 500;
            base_response::BaseResponse* baseResponse = response.mutable_base_resp();
            baseResponse->set_error_desc(e.what());
            baseResponse->set_success(false);
            std::string response_str;
            if(response.SerializeToString(&response_str))
            {
                res.body = response_str;
                res.set_header("Content-Type", "application/protobuf");
            }
        }
    });

    server.listen("0.0.0.0",8085);
}